package cn.com.git.admin.api;

import cn.com.git.admin.config.security.LoginContext;
import cn.com.git.admin.service.SystemService;
import cn.com.git.admin.service.dto.SysNoteDictListDTO;
import cn.com.git.admin.service.dto.SysNoteDictDetailDTO;
import cn.com.git.admin.service.dto.SysNoteDictSaveDTO;
import cn.com.git.admin.service.query.SysNoteDictListQuery;
import cn.com.git.common.base.BaseApi;
import cn.com.git.common.model.CommonStateConstant;
import cn.com.git.common.model.ResultConstant;
import cn.com.git.common.model.ResultMsg;
import cn.com.git.common.model.ResultPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * @author zhangzhenfu
 * @Description
 * @date 2020/5/15 16:43
 */
@Api(value = "公共码表", description = "公共码表")
@RestController
@RequestMapping("/sys_note_dict")
public class SysNoteDictApi extends BaseApi {

    @Autowired
    private LoginContext loginContext;

    @Autowired
    private SystemService systemService;

    @ApiOperation(value = "列表查询", notes = "列表查询", httpMethod = "POST",consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping(value = "/list/page",consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultPage<SysNoteDictListDTO> listPage(@Valid @RequestBody SysNoteDictListQuery query) {
        ResultPage<SysNoteDictListDTO> resultPage ;
        try {
            resultPage = systemService.listSysNoteDict(query);
        }catch (Throwable e){
            resultPage = new ResultPage<>(ResultConstant.FAILED);
            logger.error("列表查询失败：{}",e.getMessage());
        }
        return resultPage ;
    }

    @GetMapping(value = "/find",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "查询", httpMethod = "GET", notes = "根据id查询数据", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, example = "1", paramType = "query", dataType = "integer"),
    })
    public ResultMsg<SysNoteDictDetailDTO> find(@NotNull(message = "id不能为空") Integer id) {
        ResultMsg<SysNoteDictDetailDTO> resultMsg ;
        try {
            resultMsg = systemService.findSysNoteDict(id);
        }catch (Throwable e){
            resultMsg = new ResultMsg<>(ResultConstant.FAILED);
            logger.error("查询失败：{}",e.getMessage());
        }
        return resultMsg ;
    }


    @ApiOperation(value = "新增", notes = "新增", httpMethod = "POST", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping(value = "/add", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultMsg<SysNoteDictSaveDTO> add(@Valid @RequestBody SysNoteDictSaveDTO form) {
        ResultMsg<SysNoteDictSaveDTO> resultMsg = null ;
        try {
            resultMsg = systemService.addSysNoteDict(form);
        }catch (Throwable e){
            resultMsg = new ResultMsg<>(ResultConstant.FAILED);
            logger.error("新增失败：{}",e.getMessage());
        }
        return resultMsg ;
    }
    @ApiOperation(value = "编辑", notes = "编辑", httpMethod = "POST", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping(value = "/edit", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultMsg<SysNoteDictSaveDTO> edit(@Valid @RequestBody SysNoteDictSaveDTO form) {
        ResultMsg<SysNoteDictSaveDTO> resultMsg ;
        try {
            resultMsg = systemService.editSysNoteDict(form);
        }catch (Throwable e){
            resultMsg = new ResultMsg<>(ResultConstant.FAILED);
            logger.error("编辑失败：{}",e.getMessage());
        }
        return resultMsg ;
    }

    @GetMapping(value = "/delete",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "删除", httpMethod = "GET", notes = "根据id删除数据",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, example = "1", paramType = "query", dataType = "integer"),
    })
    public ResultMsg delete(@NotNull(message = "id不能为空") Integer id) {
        ResultMsg resultMsg ;
        try {
            resultMsg = systemService.deleteSysNoteDict(id);
        }catch (Throwable e){
            resultMsg = new ResultMsg<>(ResultConstant.FAILED);
            logger.error("删除失败：{}",e.getMessage());
        }
        return resultMsg ;
    }


}
